<?php $this->load->view('admin_home/_header') ?>
<?php $this->load->view('message') ?>

<script type="text/javascript" src="<?php echo base_url()?>js/fancybox/jquery.fancybox.pack.js"></script>
<link type="text/css" rel="stylesheet" href="<?php echo base_url()?>css/fancybox/jquery.fancybox.css"/>

<script type="text/javascript" src="<?php echo base_url()?>js/ajaxfileupload.js"></script>

<script type="text/javascript">
	var leaveType="notSubmit";
  	$().ready(function(){
    	$("#test_point_li").addClass("active"); 

		$(".fancybox-effects").fancybox({
				padding: 0,

				openEffect : 'elastic',
				openSpeed  : 150,

				closeEffect : 'elastic',
				closeSpeed  : 150,

				closeClick : true,

				helpers : {
					overlay : null
				}
			});

		//表单验证的内容
		$("#knowledge_point_add_form").validate({
			rules:{
				content:
				{
					required:true,
					maxlength:400
				}
			}
		}); 

		//离开页面的时候告知服务器删除临时上传图片的数据
		window.onbeforeunload = function (event)
        {
        	if(leaveType!='submit'){
        		myUrl="<?php echo site_url('admin_knowledge_point/delete_temp_picture').'/'.$time_stamp?>";
	            $.ajax({
	                url: myUrl,
	                success: function (result) {
	                },
	                error: function () {
	                }
	            });

            return "提示：服务器上的图片已经被删除";
            }
        }
	});


	
	function ajaxFileUpload()
	{
		$.ajaxFileUpload({
	        url:"<?php echo site_url('admin_knowledge_point/ajax_file_upload').'/'.$time_stamp.'/pic' ?>",
	        secureuri:false,
	        fileElementId:'pic_input',
	        success: function(json,status){   
	        	$("#pic_img").attr("src","<?php echo base_url() ?>uploads/exp/<?php echo $time_stamp?>/pic_thumb.jpg?"+Math.random());
	        	$("#pic_a").attr("href","<?php echo base_url() ?>uploads/exp/<?php echo $time_stamp?>/pic.jpg?"+Math.random());
	        	$("#pic_button").removeAttr("disabled");
	        	
	        },
	        error: function(data,status,e){
	            //print error
	        }
    	});
    	return false;
	}

	function delete_pic(file)
	{

		myUrl="<?php echo site_url('admin_knowledge_point/ajax_delete_pic').'/'.$time_stamp ; ?>";
		//alert(myUrl);
		$.ajax({
			type:"post",
			url:myUrl,
			success:function()
			{
				$("#pic_img").attr("src","<?php echo base_url() ?>uploads/empty.jpg");
	        	$("#pic_a").attr("href","<?php echo base_url() ?>uploads/empty.jpg");
			}
		});
		$("#pic_"+file+"_button").attr("disabled","disabled");
	}


	function form_submit()
	{
		leaveType="submit";
		$("#knowledge_point_add_form").submit();
	}

</script>

<div class="container">
	<div class="panel panel-primary">
		<div class="panel-heading">
			<h3 class="panel-title">添加知识点</h3>
		</div>
		<div class="panel-body">
			<?php echo validation_errors(); ?>
			<form id="knowledge_point_add_form" class="form-horizontal" action="<?php echo site_url('admin_knowledge_point/add')?>" method="post">
				<input type="text" id="test_point_id" name="test_point_id" value="<?=$test_point_id?>" class="hidden">
				<input type="text" name="time_stamp" value="<?=$time_stamp?>" class="hidden">
				<fieldset>
					<div class="form-group">
						<label class="col-lg-2 control-label">内容:</label>
						<div class="col-lg-7">
							<input class="form-control" type="text" id="content" name="content"/>
						</div>
					</div>
					<div class="form-group">
						<label class="col-lg-2 control-label">解释:</label>
						<div class="col-lg-6">
							<textarea class="form-control" rows="4" id="explanation" name="explanation"></textarea>
						</div>
						<div class="col-lg-1">
							<div id="pic_div">	
								<a id="pic_a" class="fancybox-effects">
									<img id="pic_img" src="">
								</a>
							</div>
						</div>
						<div class="col-lg-3">
							<input id="pic_input" type="file" name="pic_input" size="20" onchange="ajaxFileUpload()" />
							<button type='button' id="pic_button" class="btn btn-primary" disabled="disabled" onclick="delete_pic()">删除</button>
						</div>
					</div>
					

					<div class="form-group">
						<label class="col-lg-2 control-label"></label>
						<div class="col-lg-8">
							<button onclick="form_submit()" class="btn btn-primary">提交</button>
						</div>
					</div>
				</fieldset>
			</form> 
		</div>
		
	</div>
</div>

<?php $this->load->view('admin_home/_footer')?>


